package com.qqtech.qquan.user.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.draw.enums.UserDrawStatusDictEnum;
import com.qqtech.qquan.draw.model.DrawBaseQuery;
import com.qqtech.qquan.draw.model.DrawBaseVo;
import com.qqtech.qquan.draw.model.DrawItemVo;
import com.qqtech.qquan.draw.service.DrawBaseService;
import com.qqtech.qquan.draw.service.DrawItemService;
import com.qqtech.qquan.draw.service.DrawSysDictService;
import com.qqtech.qquan.draw.service.DrawSysParamService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserDraw;
import com.qqtech.qquan.user.model.UserDrawQuery;
import com.qqtech.qquan.user.model.UserDrawVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserDrawService;

@Controller
@RequestMapping(value = "/user/draw")
public class UserDrawController extends BaseAdminController {
	@Resource
	private UserDrawService userDrawService;
	
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private DrawItemService drawItemService;
	
	@Resource
	private DrawSysParamService drawSysParamService;
	
	@Resource
	private DrawSysDictService drawSysDictService;
	
	@Resource
	private DrawBaseService drawBaseService;
    
	@Override
	protected BaseService<UserDraw> getBaseService() {
		return userDrawService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		DrawBaseQuery drawBaseQuery=new DrawBaseQuery();
		List<DrawBaseVo> list=drawBaseService.queryList(drawBaseQuery);
		model.addAttribute("list", list);
		return "/user/draw/userDraw-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(UserDrawQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<UserDrawVo> page = userDrawService.queryPageListAdmin(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/detail/{id}/{userId}/{drawItemId}")
	public String detail(@PathVariable Integer id,@PathVariable Integer userId,@PathVariable Integer drawItemId,Model model) {
		UserDrawVo detailVo = userDrawService.getById(id);
		UserBaseVo detailVo1 = userBaseService.getById(userId);
		DrawItemVo detailVo2 = drawItemService.getById(drawItemId);
		String category="";
		model.addAttribute("category", category);
		model.addAttribute("detailVo", detailVo);
		model.addAttribute("detailVo1", detailVo1);
		model.addAttribute("detailVo2", detailVo2);
		return "/user/draw/userDraw-detail";
	}
	
	/**
	 * 创建成功
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(UserDraw addDraw) {
		Result ret = new Result();
		userDrawService.insert(addDraw);
		return ret;
	}

	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/user/draw/userDraw-add";
	}
	
	/**
	 * 去更新
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		UserDrawVo userDrawVo = userDrawService.getById(id);
		model.addAttribute("userDrawVo", userDrawVo);
		UserDrawQuery userDrawQuery = new UserDrawQuery();
		List<UserDrawVo> list = userDrawService.queryList(userDrawQuery);
		model.addAttribute("list", list);
		return "/user/draw/userDraw-update";
	}
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(UserDraw userDraw) {
		Result ret = new Result();
		userDrawService.updateById(userDraw	);
		return ret;
	}
	
	/**
	 * 未领取
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/unclaimed")
	@ResponseBody
	public Result unclaimed(Integer id) {
		Result ret = new Result();
		UserDrawVo userDrawVo =new UserDrawVo();
		userDrawVo.setId(id);
		userDrawVo.setStatus(UserDrawStatusDictEnum.ONTRECEIVE.getCode());
		userDrawService.updateById(userDrawVo);
		return ret;
	}
	
	/**
	 * 已编辑
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/receive")
	@ResponseBody
	public Result receive(Integer id) {
		Result ret = new Result();
		UserDrawVo userDrawVo =new UserDrawVo();
		userDrawVo.setId(id);
		userDrawVo.setStatus(UserDrawStatusDictEnum.RECEIVE.getCode());
		userDrawService.updateById(userDrawVo);
		return ret;
	}
}
